<template>
  <div class="ft-form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :label-width="labelWidth">
      <el-row>
        <template v-for="(item, index) in formItems" :key="index">
          <el-col v-bind="colLayout">
            <el-form-item
              :label="item.label"
              :rules="item.rules"
              class="form-item"
              :style="itemStyle"
              v-if="!item.isHidden"
            >
              <template
                v-if="item.type === 'input' || item.type === 'password'"
              >
                <el-input
                  v-model="formData[`${item.field}`]"
                  :placeholder="item.placeHolder"
                  :show-password="item.type === 'password'"
                />
              </template>
              <template v-else-if="item.type === 'textarea'">
                <el-input
                  v-model="formData[`${item.field}`]"
                  :rows="2"
                  type="textarea"
                  :placeholder="item.placeHolder"
                  style="width: 100%"
                />
              </template>
              <template v-else-if="item.type === 'select'">
                <el-select
                  v-model="formData[`${item.field}`]"
                  clearable
                  :placeholder="item.placeHolder"
                  style="width: 100%"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :value="option.value"
                    >{{ option.label }}</el-option
                  >
                </el-select>
              </template>
              <template v-else-if="item.type === 'phone'">
                <el-select
                  placeholder="select country code"
                  filterable
                  style="width: 50%"
                  v-model="value"
                >
                  <el-option-group
                    v-for="group in country"
                    :key="group.label"
                    :label="group.label"
                  >
                    <el-option
                      v-for="item in group.options"
                      :key="item.short"
                      :label="`${item.en}(${item.tel})`"
                      :value="item.tel"
                    >
                    </el-option>
                  </el-option-group>
                </el-select>
                <el-input
                  v-model="formData[`${item.field}`]"
                  :placeholder="item.placeHolder"
                >
                </el-input>
              </template>
              <template v-else-if="item.type === 'cascader'">
                <el-cascader
                  :options="item.options"
                  :placeholder="item.placeHolder"
                  style="width: 100%"
                  clearable
                  :show-all-levels="false"
                  v-model="formData[`${item.field}`]"
                ></el-cascader>
              </template>
              <template v-else-if="item.type === 'datepicker'">
                <el-date-picker
                  v-model="formData[`${item.field}`]"
                  v-bind="item.otherOption"
                  style="width: 100%"
                >
                </el-date-picker>
              </template>
              <template v-else-if="item.type === 'upload'">
                <slot name="upload"></slot>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, watch } from 'vue'
import { IFormItem } from '../types'
import { country } from '../data/countrycode'

export default defineComponent({
  props: {
    modelValue: {
      type: Object
    },
    labelWidth: {
      type: String,
      default: () => '80px'
    },
    formItems: {
      type: Array as PropType<IFormItem[]>,
      default: () => []
    },
    itemStyle: {
      type: Object,
      default: () => ({ padding: '10px 40px' })
    },
    colLayout: {
      type: Object,
      default: () => ({
        xl: 6, // ≥1920px
        lg: 8, // ≥1200px
        md: 12, // ≥992px
        sm: 24, // ≥768px
        xs: 24 // <768px
      })
    }
  },
  emit: ['update:modelValue'],
  setup(props, { emit }) {
    const formData = ref({ ...props.modelValue })
    const value = ref('')

    watch(
      formData,
      (newValue) => {
        //组件的v-model其实是 :model-valud="xx" @update:model-value = "xx = $event"
        emit('update:modelValue', newValue)
      },
      { deep: true }
    )

    return {
      formData,
      country,
      value
    }
  }
})
</script>

<style scoped lang="less">
.ft-form {
  padding: 20px 20px 0;
}
</style>
